package me.hys.api.company;

import java.util.List;
import java.util.Map;

import me.hys.api.BaseApi;
import me.hys.base.PageQuery;
import me.hys.entity.company.ProductEntity;
import me.hys.service.company.IProductManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-19 上午12:59:40 . <br>
 * Function: 产品API
 */
@Controller
@RequestMapping("/api/product")
public class ProductApi extends BaseApi {
	@Autowired
	private IProductManager productManager;

	/**
	 * @Title : detail
	 * @Description : 详情，jsp页面
	 */
	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public ModelAndView detail(@PathVariable String id) {
		Map<String, Object> data = Maps.newHashMap();
		data.put("entity", productManager.findById(id));
		return new ModelAndView("/api/product", data);
	}

	/**
	 * @Title : get
	 * @Description : 查询详情
	 */
	@ResponseBody
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public ProductEntity get(@PathVariable String id) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("id", id);
		params.put("userId", request.getParameter("userId"));
		return productManager.findById(params);
	}

	/**
	 * @Title : latest
	 * @Description : 最新一条记录
	 */
	@ResponseBody
	@RequestMapping(value = "/latest", method = RequestMethod.GET)
	public ProductEntity latest() {
		return productManager.findLatest();
	}

	/**
	 * @Title : list
	 * @Description : 查询列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list/{start}", method = RequestMethod.GET)
	public List<ProductEntity> list(@PathVariable int start) {
		PageQuery<ProductEntity> page = getPageQuery(start);
		page.getCondition().put("userId", request.getParameter("userId"));
		PageQuery<ProductEntity> result = productManager.findPage(page);
		return result.getList();
	}

	/**
	 * @Title : list
	 * @Description : 查询列表
	 */
	@ResponseBody
	@RequestMapping(value = "/list/{userId}/{start}", method = RequestMethod.GET)
	public List<ProductEntity> list(@PathVariable int userId,
			@PathVariable int start) {
		PageQuery<ProductEntity> page = getPageQuery(start);
		page.getCondition().put("userId", userId);
		PageQuery<ProductEntity> result = productManager.findPage(page);
		return result.getList();
	}
}
